using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

using Deepcode.Flux.Core.Services.Configuration;
using Deepcode.Flux.Core.Systems.Security;

namespace Deepcode.Flux.Core.Systems.URLRewriter
{
	public class URLRewriterModule : IHttpModule
	{
		public void Dispose()
		{
		}

		public void Init(HttpApplication context)
		{
			switch (FluxConfig.Authentication)
			{
				case FluxAuthenticationMode.FORMS:
					context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);
					break;
				case FluxAuthenticationMode.WINDOWS:
					context.BeginRequest += new EventHandler(context_BeginRequest);
					break;
			}
		}

		/// <summary>
		/// Begin request is executed for windows authentication re-writing
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void context_BeginRequest(object sender, EventArgs e)
		{
			HttpApplication app = (HttpApplication) sender;
			Rewriter rw = new Rewriter();
			rw.DoRewriting( app );
		}

		/// <summary>
		/// Authorise request is executed for forms authentication re-writing
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void context_AuthorizeRequest(object sender, EventArgs e)
		{
			HttpApplication app = (HttpApplication) sender;
			Rewriter rw = new Rewriter();
			rw.DoRewriting( app );
		}
	}
}
